package com.itheima.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//配置类 ：封装与es的连接信息
//启动主类就会自动加载@Configuration注解下的类
@Configuration
public class ElasticsearchConfig {
    @Value("${heima.elasticsearch.hostlist}")
    private String hostlist;

    //1 获取连接的客户端  连接es
    @Bean(destroyMethod = "close")
    public RestHighLevelClient restHighLevelClient(){
        String[] split=hostlist.split(",");
        HttpHost[] httpHostsArray=new HttpHost[split.length];
        for(int i=0;i<split.length;i++){
            String item=split[i];
            httpHostsArray[i]=new HttpHost(item.split(":")[0],Integer.parseInt(item.split(":")[1]),"http");
        }
        return new RestHighLevelClient(RestClient.builder(httpHostsArray));
    }

}

//配置
